#!/usr/bin/env bash

jailname=`echo $1 | sed 's/\./_/g'`
msdelay=$2
jailID=`qjail list $jailname | perl -ane 'print "$F[1]\n" if /\d+/'` || exit 1

ops-in(){
    local setID=10
    local ruleID=$jailID
    local direction="in"

    ipfw delete set $setID $ruleID
    ipfw add $ruleID set $setID pipe 1 ip from any to any $direction jail $jailID || exit 1
    ipfw pipe 1 config delay ${msdelay}ms || exit 1
}

ops-out(){
    local setID=11
    local ruleID=$jailID
    local direction="out"

    ipfw delete set $setID $ruleID
    ipfw add $ruleID set $setID pipe 1 ip from any to any $direction jail $jailID || exit 1
    ipfw pipe 1 config delay ${msdelay}ms || exit 1
}

ops-in
ops-out
